package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.constant.ResultCode;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.UnitConversion;
import com.synergy.stockflow.product.entity.Workshop;
import com.synergy.stockflow.product.service.WorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/workshop")
public class WorkshopController {
    @Autowired
    WorkshopService workshopService;
    @GetMapping("/index")
    public Result<Page<Workshop>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                       @RequestParam(name = "size", defaultValue = "5") int size,
                                       @RequestParam(name = "workshopcId", defaultValue = "") String workshopcId,
                                       @RequestParam(name = "workshopName", defaultValue = "") String workshopName
    ) {
        System.out.println(workshopcId);
        QueryWrapper<Workshop> query = new QueryWrapper<>();
        if (workshopcId != null && workshopcId.length() > 0) {
            query.lambda().like(Workshop::getWorkshopcId, workshopcId);
        }
        if (workshopName != null && workshopName.length() > 0) {
            query.lambda().like(Workshop::getWorkshopName, workshopName);
        }

        Page<Workshop> page = new Page<>(current, size);
        page = workshopService.page(page, query);
//        List<Material> list = new ArrayList<>();
//        for (Material record : page.getRecords()) {
//            record.setBoms(bomService.selectspecificationsAndModels(record.getMaterialId()));
//        }
//        page.setRecords(list);
        return Result.success(page);
    }
    @PostMapping("/insert")
    public Result insert(@RequestBody Workshop workshop) {
        QueryWrapper<Workshop> query = new QueryWrapper<>();
        if (workshop.getWorkshopcId() != null && workshop.getWorkshopcId().length() > 0) {
            query.lambda().eq(Workshop::getWorkshopcId, workshop.getWorkshopcId());
        }
        query.lambda().orderBy(true,false, Workshop::getCreateTime);
        Page<Workshop> page = new Page<>(1, 10000);
        Page<Workshop> page1 = workshopService.page(page, query);
        System.out.println(page1);
        if (page1 == null || page1.getRecords().size() == 0) {
            workshop.setCreateTime(new Date());
            workshopService.save(workshop);
            return Result.success(workshop);
        }
        return Result.error(ResultCode.BUSINESS_EXCEPTION.value(), "车间编码已存在");

    }

    @PutMapping("/update")
    public Result update(@RequestBody Workshop workshop) {
        workshop.setUpdateTime(new Date());
        workshopService.updateById(workshop);

        return Result.success(workshop);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<String> workshopIds) {
        workshopService.removeBatchByIds(workshopIds);
        return Result.success(workshopIds);
    }
}

